package com.example.planschedulingsystem.service;

import com.example.planschedulingsystem.entity.PlaneTrip;
import com.example.planschedulingsystem.repository.PlaneTripRepository;
import com.example.planschedulingsystem.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class PlaneTripManagerService {
    @Autowired
    private PlaneTripRepository planeTripRepository;

    public PlaneTrip save(PlaneTrip trip) { return planeTripRepository.save(trip); }

    public List<PlaneTrip> findAllByPlaneId(Long plane_id) {
        DateUtil dateUtil = new DateUtil();
        Timestamp start = dateUtil.getToDayStart();
        Timestamp end = dateUtil.getToDayEnd();
        return planeTripRepository.findPlaneTripsByPlane_IDAndDeparturetimeAfterAndDeparturetimeBeforeOrderByDeparturetimeAsc(plane_id, start, end);
    }

    public List<PlaneTrip> findAllByDeparturetimeAfterAndDeparturetimeBefore() {
        DateUtil dateUtil = new DateUtil();
        Timestamp start = dateUtil.getToDayStart();
        Timestamp end = dateUtil.getToDayEnd();
        return planeTripRepository.findAllByDeparturetimeAfterAndDeparturetimeBeforeOrderByDeparturetimeAsc(start, end);
    }

    public PlaneTrip saveOrUpdate(PlaneTrip trip) {
        return planeTripRepository.save(trip);
    }

    @Modifying
    @Transactional
    public Long delete(Long ID) {
        return planeTripRepository.deletePlaneTripByID(ID);
    }
}
